package com.SouthernWall_404.Age_Capitalism.common.Gui.Slots;

import com.SouthernWall_404.Age_Capitalism.Client.Gui.Screen.ModMerchantScreen;
import com.SouthernWall_404.Age_Capitalism.common.World.Entity.StoreBlockEntity;
import net.minecraft.core.NonNullList;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.MerchantContainer;
import net.minecraft.world.inventory.MerchantResultSlot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.trading.Merchant;
import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.world.item.trading.MerchantOffers;
import net.minecraft.world.level.Level;

public class ModMerchantResultSlot extends MerchantResultSlot {

    private Merchant merchant;
    private StoreBlockEntity storeBlockEntity;
    private final MerchantContainer slots;

    /*
    记得写quickStack和收钱
     */
    public ModMerchantResultSlot(Player p_40083_, Merchant merchant, MerchantContainer merchantContainer, int p_40086_, int p_40087_, int p_40088_, StoreBlockEntity storeBlockEntity) {
        super(p_40083_, merchant, merchantContainer, p_40086_, p_40087_, p_40088_);

        this.merchant=merchant;
        this.slots = merchantContainer;

        Level level=p_40083_.level();
        if(level.isClientSide) {

        }
        else {
            this.storeBlockEntity=storeBlockEntity;
        }
    }

    @Override
    public void onTake(Player player, ItemStack itemStack) {
//        if (player.level().isClientSide) {
//            return; // 客户端不处理
//        }

        if(this.storeBlockEntity!=null)//这里存在问题
        {
//            player.sendSystemMessage(Component.translatable(player.level().toString()));
            int toCost=itemStack.getCount();
//            boolean hasResult=true;//用于判断是否还有物品未消耗掉
            NonNullList<ItemStack> chest=this.storeBlockEntity.getContainer();
            for(int i=0;i<chest.size();i++) {//遍历所有箱子物品
                ItemStack current =chest.get(i);
                if(ItemStack.isSameItemSameTags(current,itemStack)) {//若与需要消耗的一致
                    int todo=Math.min(current.getCount(),itemStack.getCount());//获取木桶短板，即需要消耗的数量
                    current.shrink(todo);//消耗
                    toCost-=todo;

                    if(toCost<=0) {//消耗过后，检查
                        break;//若已全部消耗完毕，停止遍历
                    }
                }
            }

            MerchantOffer offer = this.slots.getActiveOffer();//获取offer,这里需要注意，不排除为空的情况
            if (offer != null) {
                ItemStack costA = offer.getCostA();
                ItemStack costB = offer.getCostB();
                int toGainA=costA.getCount();
                int toGainB=costB.getCount();

                for(int i=0;i<chest.size();i++){//再次进行遍历
                    ItemStack current=chest.get(i);
                    if(current.isEmpty()){//若为空
                        if(toGainA>0) {//如果A还没放完
                            int todo=Math.min(costA.getMaxStackSize(),toGainA);//获取木桶短板，即需要消耗的数量

                            ItemStack toPlace=costA.copy();//copy防止丢失nbt数据
                            toPlace.setCount(todo);//设置数量

                            chest.set(i,toPlace);//在此放置A
                            this.storeBlockEntity.setContainer(chest);
                            toGainA-=todo;
                        }
                        else if(toGainB>0)
                        {
                            int todo=Math.min(costB.getMaxStackSize(),toGainB);//获取木桶短板，即需要消耗的数量

                            ItemStack toPlace=costB.copy();//copy防止丢失nbt数据
                            toPlace.setCount(todo);//设置数量

                            chest.set(i,toPlace);//在此放置B
                            this.storeBlockEntity.setContainer(chest);
                            toGainB-=todo;
                        }
                    }
                    if(ItemStack.isSameItemSameTags(current,costA)&&current.getCount()<current.getMaxStackSize()) {//若为A且未满
                        int todo=Math.min(costA.getMaxStackSize()-current.getCount(),toGainA);
                        current.grow(todo);
                        this.storeBlockEntity.setContainer(chest);
                        toGainA-=todo;
                    }
                    if(ItemStack.isSameItemSameTags(current,costB)&&current.getCount()<current.getMaxStackSize()) {//若为B且未满
                        int todo=Math.min(costB.getMaxStackSize()-current.getCount(),toGainB);
                        current.grow(todo);
                        this.storeBlockEntity.setContainer(chest);
                        toGainB-=todo;
                    }

                    if(toGainA<=0&&toGainB<=0) {//放置过后，检查
                        break;//若已全部放置完毕，停止遍历
                    }
                }
            }





        }
        super.onTake(player, itemStack);
    }

    //    @Override
//    public void onTake(Player p_150631_, ItemStack itemStack) {
//        super.onTake(p_150631_, itemStack);
//        if (!p_150631_.level().isClientSide) {
//            NonNullList<ItemStack> items =this.storeBlockEntity.getContainer();
//            int needToSell=itemStack.getCount();
//
//            while (needToSell>0){//若还有剩余未分配的待分配物品
//                for(int i=0;i<items.size();i++) {
//                    ItemStack currentItemStack = items.get(i);
//                    if (ItemStack.isSameItemSameTags(itemStack, currentItemStack)) {
//                        int removeCount = itemStack.getCount() < currentItemStack.getCount() ? itemStack.getCount() : currentItemStack.getCount();
////                        itemStack.shrink(removeCount);
//                        currentItemStack.shrink(removeCount);
//                        needToSell -= removeCount;
//                        MerchantOffer offer = this.slots.getActiveOffer();
//
////                        this.storeBlockEntity.notifyTrade(offer);
//                    }
//                    if (needToSell <= 0) {
//                        break;
//                    }
//                }
//
//                MerchantOffer offer = this.slots.getActiveOffer();
//                int needToGainA=itemStack.getCount()/offer.getResult().getCount()*offer.getCostA().getCount();
//                int needToGainB=itemStack.getCount()/offer.getResult().getCount()*offer.getCostB().getCount();
//                boolean hasA=true;
//                boolean hasB=true;
//
//                for(int i=0;i<items.size();i++){
//                    ItemStack currentItemStack = items.get(i);
//                    if(currentItemStack.isEmpty()){//当前格子为空格
//                        if(hasA){//若A未完成放置
//                            int toPlaceAmount=Math.min(needToGainA,offer.getCostA().getMaxStackSize());//得到可放置上限
//                            currentItemStack=new ItemStack(offer.getCostA().getItem(),toPlaceAmount).copy();//按照放置上限放置
//                            items.set(i,currentItemStack);
//                            this.storeBlockEntity.setContainer(items);//回传，设置
//
//                            needToGainA-=toPlaceAmount;//减少待放置的量
//
//                            if(needToGainA<=0)//若没有待放置
//                            {
//                                hasA=false;//停止
//                            }
//                        }
//                        else if(hasB) {
//                            int toPlaceAmount=Math.min(needToGainB,offer.getCostB().getMaxStackSize());//同上
//                            currentItemStack=new ItemStack(offer.getCostB().getItem(),toPlaceAmount);
//                            items.set(i,currentItemStack);
//                            this.storeBlockEntity.setContainer(items);
//
//                            needToGainB-=toPlaceAmount;
//
//                            if(needToGainB<=0) {
//                                hasB=false;
//                            }
//                        }
//                        else{//AB均放置完成
//                            return;//停止
//                        }
//
//                    }
//                    if(ItemStack.isSameItemSameTags(currentItemStack,offer.getCostA())){//若存在A
//                        if(currentItemStack.getCount()<offer.getCostA().getMaxStackSize()){//未满
//                            if(hasA){//若还有待放置A
//                                int toPlaceAmount=Math.min(needToGainA,offer.getCostA().getMaxStackSize()-currentItemStack.getCount());//得到可放置上限
//                                currentItemStack.grow(toPlaceAmount);
//
//                                needToGainA-=toPlaceAmount;
//                                if(needToGainA<=0) {
//                                    hasA=false;
//                                }
//                            }
//                        }
//                    }
//                    if(ItemStack.isSameItemSameTags(currentItemStack,offer.getCostB())){//若存在A
//                        if(currentItemStack.getCount()<offer.getCostB().getMaxStackSize()){//未满
//                            if(hasB){//若还有待放置A
//                                int toPlaceAmount=Math.min(needToGainB,offer.getCostB().getMaxStackSize()-currentItemStack.getCount());//得到可放置上限
//                                currentItemStack.grow(toPlaceAmount);
//
//                                needToGainB-=toPlaceAmount;
//                                if(needToGainB<=0) {
//                                    hasB=false;
//                                }
//                            }
//                        }
//                    }
//
//                }*
//                this.storeBlockEntity.notifyTrade(offer);
//            }
//        }
//    }
//
//
//    public void QuickCraft(Player player) {
//
//        if (!player.level().isClientSide) {
//
//
//            this.onTake(player,this.slots.getActiveOffer().getResult());
//
//
//        }
//
//
//
//    }
}
